package com.lzy.webapi.controller;

import com.lzy.base.apis.DeptServiceApi;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.QiniuUtils;
import com.lzy.member.dtos.MemberAccountDto;
import com.lzy.product.apis.ProductUserServiceApi;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.OrderProductDto;
import com.lzy.product.dtos.ProductUserDto;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.MemberFacade;
import com.lzy.webapi.facade.ProductUserFacade;
import com.lzy.webapi.response.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("api/user")
public class ProductUserController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductUserController.class);
    @Autowired
    ProductUserServiceApi productUserServiceApi;
    @Autowired
    DeptServiceApi deptServiceApi;
    @Autowired
    ProductUserFacade productUserFacade;
    @Autowired
    MemberFacade memberFacade;

    /**
     * 个人信息接口
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/profile", method = RequestMethod.GET)
    @ResponseBody
    public Result getProfile(@RequestHeader("token") String token) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        ProductUserResponse userResponse = productUserFacade.getProfile(ut.getUserId());
        return new Result(userResponse);
    }

    /**
     * 修改用户头像
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/updateAvatar", method = RequestMethod.POST)
    @ResponseBody
    public Result updateUser(@RequestHeader("token") String token, String avatar) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        ProductUserDto dto = new ProductUserDto();
        dto.setId(ut.getUserId());
        dto.setAvatar(avatar);
        Boolean flag = productUserServiceApi.update(dto);
        Ensure.that(flag).isTrue("01000007");
        return new Result();
    }


    /**
     * 查看团队成员列表
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/team", method = RequestMethod.GET)
    @ResponseBody
    public Result getTeam(@RequestHeader("token") String token) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        TeamResponse response = productUserFacade.getTeam(ut.getUserId());
        return new Result(response);
    }

    /**
     * 查看下级成员信息
     *
     * @param token
     * @param id
     * @return
     */
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Result getTeamInfo(@RequestHeader("token") String token, @PathVariable("id") Long id) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        TeamInfoResponse response = productUserFacade.getTeamInfo(id);
        return new Result(response);
    }

    /**
     * 获取七牛token
     *
     * @throws
     * @Title: token
     * @param: bucketName
     * @return: Result
     */
    @RequestMapping(value = "/token", method = RequestMethod.GET)
    @ResponseBody
    public Result getWebUpToken(String bucketName) {
        WebUpTokenResponse webUpTokenResponse = new WebUpTokenResponse();
        webUpTokenResponse.setPath(QiniuUtils.makeHttpsBaseUrl());
        webUpTokenResponse.setWebUpToken(QiniuUtils.getWebUptoken(bucketName));
        return new Result(webUpTokenResponse);
    }

    /**
     * 验证用户是否登录
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/loginStatus", method = RequestMethod.GET)
    @ResponseBody
    public Result checkIsLogin(@RequestHeader("token") String token) {
        Integer flag = 0;
        UserToken ut = getUserToken(token);
        if (ut != null) {
            flag = 1;
        }
        return new Result(flag);
    }

    /**
     * 查询股权数量
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/selectStock", method = RequestMethod.GET)
    @ResponseBody
    public Result selectStock(@RequestHeader String token) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        MemberAccountDto dto = new MemberAccountDto();
        dto.setMemberId(ut.getUserId());
        dto.setType(3);
        StockResponse response = memberFacade.getStock(dto);
        return new Result(response);
    }

    /**
     * 获取股权记录列表
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getStockList", method = RequestMethod.GET)
    @ResponseBody
    public Result getStockList(@RequestHeader("token") String token) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        OrderProductDto dto = new OrderProductDto();
        dto.setMemberId(ut.getUserId());
        dto.setStatus(2);
        dto.setType(BusinessType.OrderProductType.PRODUCT.getCode());
        List<MemberStockResponse> responses = memberFacade.getStockLogList(dto);
        return new Result(responses);
    }

    /**
     * 获取股权记录列表
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getStockDetail/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Result getStockDetail(@RequestHeader("token") String token, @PathVariable("id") Long id) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        MemberStockDetailResponse responses = memberFacade.getStockDetail(id);
        return new Result(responses);
    }


}
